package com.twu.biblioteca.command;

import com.twu.biblioteca.LibraryController;
import com.twu.biblioteca.model.Book;
import com.twu.biblioteca.model.Movie;
import com.twu.biblioteca.model.User;

public class ListTitlesCommand implements Command{

    private LibraryController libraryController;

    public ListTitlesCommand(LibraryController libraryController) {
        this.libraryController = libraryController;
    }

    @Override
    public void execute() {
        StringBuilder sb = new StringBuilder();
        sb.append("All books: \n");
        for (Book book : libraryController.getBooks()){
            sb.append("  ID: ").append(book.getId()).append(" Name: ").append(book.getName());
            sb.append("  Author: ").append(book.getAuthor()).append(" Year Published: ").append(book.getYear());

            if (!book.isAvailable()){
                User user = libraryController.getUserByTitle(book);
                sb.append(" Rented by:").append(user.getName());
            }
            sb.append("\n");
        }
        sb.append("All movies: \n");
        for (Movie movie : libraryController.getMovies()){
            sb.append("  ID: ").append(movie.getId()).append(" Name: ").append(movie.getName());
            sb.append("  Director: ").append(movie.getDirector()).append(" Year Published: ").append(movie.getYear());
            sb.append("  Rating: ").append(movie.getRating());

            if (!movie.isAvailable()){
                User user = libraryController.getUserByTitle(movie);
                sb.append(" Rented by:").append(user.getName());
            }
            sb.append("\n");
        }
        System.out.println(sb.toString());

    }
}
